//
//  DynamicViewController.m
//  SelfDriving
//
//  Created by apple on 16/1/28.
//  Copyright © 2016年 apple. All rights reserved.
//
//点击动态先走DynamicMainViewController->(DynamicViewController(附近)、NearByViewController(好友)）
#import "DynamicMainViewController.h"
#import "XLSwitchView.h"
#import "NearbyViewController.h"
#import "DynamicViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "DetailsViewController.h"
#import "FriendDetailsMainVC.h"
#import "XhwWebViewController.h"
#import "BusinessService+FJ.h"
@interface DynamicMainViewController ()<XLSwitchViewDelegate>
@property (strong, nonatomic) XLSwitchView *switchViews;

@property (strong,nonatomic)UILabel *msgCountLab;
@end

@implementation DynamicMainViewController{
    MPMoviePlayerController *moviePlayer;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refDataDT" object:nil];
    [self.navigationController.navigationBar setHidden:YES];
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [[BusinessService shareInstance] getdynamic_unreadnum:dic success:^(id result) {
        if (result) {
            NSString *msgCount = result;
            if ([msgCount intValue] != 0) {
                _msgCountLab.hidden = NO;
                _msgCountLab.text = msgCount;
            }
            else {
                _msgCountLab.hidden = YES;
            }
        }
    } failure:^(id result) {
        
    }];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setHidden:NO];
}

- (void)viewDidLoad {
    [AppDelegate shareDelegate].dttype = 0;
    self.navigationController.navigationBarHidden = YES;
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(scollTop:)
     name:@"scollTop"
     object:nil];
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(toDetails:)
     name:@"toDetails"
     object:nil];
    
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(xw_toDetails:)
     name:@"xw_toDetails"
     object:nil];
    
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(FriendDetailsMainVC:)
     name:@"FriendDetailsMainVC"
     object:nil];
    
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(EnlargePicAction:)
     name:@"EnlargePicAction"
     object:nil];
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(Plays:)
     name:@"hPlay"
     object:nil];
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(toadview:)
     name:@"toadview"
     object:nil];
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(tomap:)
     name:@"tomap"
     object:nil];
    
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    _switchViews = [[XLSwitchView alloc] initWithFrame:CGRectMake(0,0,SCRE_WIDTH,SCRE_HEIGHT)];
    _switchViews.backgroundColor = [UIColor clearColor];
    _switchViews.delegate = self;
    _switchViews.btnWidth = 70;
    _switchViews.btncolor = UIColorFromRGB(0x1DA24A);
    _switchViews.inecolor = _switchViews.btncolor;
    [_switchViews createUserInterface];
    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, SCRE_WIDTH/2, 44)];
    [btn1 setTitle:@"资料" forState:UIControlStateNormal];
    [self.view addSubview:btn1];
    UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(SCRE_WIDTH/2, 64, SCRE_WIDTH/2, 44)];
    [btn2 setTitle:@"动态" forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    [self.view addSubview:_switchViews];
    
    UIButton *newBtn  = [[UIButton alloc]initWithFrame:CGRectMake(SCRE_WIDTH-85, 20, 42, 42)];
    [newBtn addTarget:self action:@selector(NewMsgAction:) forControlEvents:UIControlEventTouchUpInside];
    [newBtn setImage:[UIImage imageNamed:@"dnm"] forState:UIControlStateNormal];
    [self.view addSubview:newBtn];
    
    //xhw2018-2-7加
    _msgCountLab = [[UILabel alloc] initWithFrame:CGRectMake(SCRE_WIDTH-62, 29, 12, 12)];
    _msgCountLab.font = [UIFont systemFontOfSize:8];
    _msgCountLab.text = @"3";
    _msgCountLab.textColor = [UIColor whiteColor];
    _msgCountLab.backgroundColor = [UIColor redColor];
    _msgCountLab.textAlignment = NSTextAlignmentCenter;
    _msgCountLab.layer.masksToBounds = YES;
    _msgCountLab.layer.cornerRadius = _msgCountLab.frame.size.width/2;
    _msgCountLab.hidden = YES;
    [self.view addSubview:_msgCountLab];
    
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(SCRE_WIDTH-45, 20, 1, 20)];
    line.backgroundColor = UIColorFromRGB(0xf7f7f7);
    [self.view addSubview:line];
    
    UIButton *editBtn  = [[UIButton alloc]initWithFrame:CGRectMake(SCRE_WIDTH-40, 20, 42, 42)];
    [editBtn addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
    [editBtn setImage:[UIImage imageNamed:@"dfb"] forState:UIControlStateNormal];
    [self.view addSubview:editBtn];
}
-(void)tomap:(NSNotification *)not{
    NSLog(@"熊宏伟-DynamicMainVC->tomap通知");
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    MapViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"MapVC"];
    searchResultDetailController.locationType = 2;
    searchResultDetailController.model = not.object;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
    
}
-(void)scollTop:(NSNotification *)not{
    [[NSNotificationCenter defaultCenter ]postNotificationName:@"scollTops" object:nil];
    
}
-(void)toadview:(NSNotification *)not{
    //xhw2018-1-16改:bug13342
//    SVWebViewController *webC = [[SVWebViewController alloc] initWithAddress:not.object];
//    webC.hidesBottomBarWhenPushed = YES;
//    [self.navigationController pushViewController:webC animated:YES];
    
    XhwWebViewController * web = [[XhwWebViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    web.urlString = not.object;
    web.title = @"动态广告";
    [self.navigationController pushViewController:web animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
#pragma mark 新消息
-(void)NewMsgAction:(UIButton *)btn{
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"Dynaminc"
                                                                 bundle:[NSBundle mainBundle]];
    UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"NewMsgVC"];
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
    
}
#pragma mark 发布动态
-(void)editAction:(UIButton *)btn{
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"Dynaminc"
                                                                 bundle:[NSBundle mainBundle]];
    UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"ReleaseMainVC"];
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
    
}


#
#pragma mark 跳转到详细信息 跳转到 -- 动态详细
#pragma mark -- 谢威修改
-(void)toDetails:(NSNotification *)not {
//    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"Dynaminc"
//                                                                 bundle:[NSBundle mainBundle]];
//    DetailsViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"DetailsVC"];
//    searchResultDetailController.model = not.object;
//    [self.navigationController pushViewController:searchResultDetailController animated:YES];
    
    
}
- (void)xw_toDetails:(NSNotification *)not{
    
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"Dynaminc"
                                                                 bundle:[NSBundle mainBundle]];
    DetailsViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"DetailsVC"];
    NSDictionary *dic = not.userInfo;
    
    searchResultDetailController.model = dic[@"model"];
    searchResultDetailController.xwPicArray = dic[@"pic"];
    [self.navigationController pushViewController:searchResultDetailController animated:YES];

    
}



#pragma  mark 个人信息
-(void)FriendDetailsMainVC:(NSNotification *)not{
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"MS"
                                                                 bundle:[NSBundle mainBundle]];
    FriendDetailsMainVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"FriendDetailsMainVC"];
    searchResultDetailController.model = not.object;
    [self.navigationController pushViewController:searchResultDetailController animated:YES];
}
#pragma mark 视频播放
-(void)Plays:(NSNotification *)not{
    //    [ValidateHelper hideTabBar:[AppDelegate shareDelegate]._tableBarController];
    //    NSURL *url = [NSURL URLWithString:not.object];
    //    moviePlayer = [ValidateHelper bofang:url];
    //    [self.view addSubview:moviePlayer.view];
    //    //    [[[[UIApplication sharedApplication] windows] firstObject] addSubview:moviePlayer.view];
    //    self.navigationController.navigationBarHidden = YES;
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playVideoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    //
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playVideoFinisheds:) name:MPMoviePlayerLoadStateDidChangeNotification object:moviePlayer];
    
    UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"Dynaminc"
                                                                 bundle:[NSBundle mainBundle]];
    ShipingBFView *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"ShipingBFView"];
    searchResultDetailController.url = not.object;
    [self presentViewController:searchResultDetailController animated:YES completion:nil];
//    [self.navigationController pushViewController:searchResultDetailController animated:YES];
}
- (void)playVideoFinisheds:(NSNotification *)theNotification{
    
    
}
#pragma  mark 视频播放注销监听
- (void)playVideoFinished:(NSNotification *)theNotification{
    [ValidateHelper showTabBar:[AppDelegate shareDelegate]._tableBarController];
    // 取消监听
    
    [[NSNotificationCenter defaultCenter]
     removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    // 将视频视图从父视图中删除
    [moviePlayer.view removeFromSuperview];
}
#pragma mark 图片放大
-(void)EnlargePicAction:(NSNotification *)not{
    NSMutableArray  *imgdata = [[NSMutableArray alloc]init];
    DTModel *msg = not.object;
    for (int i= 0;i<msg.attachment.count; i++) {
        ZLPhotoPickerBrowserPhoto *photo = [[ZLPhotoPickerBrowserPhoto alloc] init];
        NSString *url = [[msg.attachment objectAtIndex:i] objectForKey:@"path"];
        if (![ValidateHelper isHttpHead:url]) {
            url = [NSString stringWithFormat:@"%@%@",img_Url,url];
        }
        photo.photoURL = [NSURL URLWithString:url];
        [imgdata addObject:photo];
    }
    
    // 图片游览器
    ZLPhotoPickerBrowserViewController *pickerBrowser = [[ZLPhotoPickerBrowserViewController alloc] init];
    // 数据源/delegate
    //    pickerBrowser.delegate = self;
    pickerBrowser.status = UIViewAnimationAnimationStatusFade;
    
    // 数据源可以不传，传photos数组 photos<里面是ZLPhotoPickerBrowserPhoto>
    pickerBrowser.photos = imgdata;
    // 是否可以删除照片
    pickerBrowser.editing = NO;
    // 当前选中的值
    pickerBrowser.currentIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    // 展示控制器
    [pickerBrowser showPickerVc:self];
    
}

-(NSInteger)numberOfItemsInSwitchView:(XLSwitchView *)switchView{
    return 2;
}
-(UIViewController *)switchView:(XLSwitchView *)switchView withViewOfItem:(NSInteger)index{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Dynaminc" bundle:nil];
    if (index == 0) {
        
        DynamicViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"DynamicsVC"];
        vc.nav = self.navigationController;
        vc.title = @"附近";
        return vc;
    }else{
        //此处的NearbyVC就对应着NearbyViewController
        NearbyViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"NearbyVC"];
        vc.nav = self.navigationController;
        vc.title = @"好友";
        return vc;
    }
    
    
}


-(void)ScrollAction:(NSInteger)index{
    NSNumber *indexs =  [NSNumber numberWithInteger:index];
    [UserDefaults writeUserDefaultObjectValue:[NSString stringWithFormat:@"%@",indexs] withKey:@"index"];
    
    
}


@end
